package com.QK.cnstudy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.QK.cnstudy.adapter.TextBookAdapter;
import com.QK.cnstudy.db.DBManager;
import com.QK.cnstudy.entity.Textbook;

public class MaterialSelectActivity extends Activity implements OnItemClickListener, OnClickListener {

    DBManager dbManager = null;
    private TextBookAdapter adapter;
    private ListView listView = null;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    Button finishButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.material_select);

        dbManager = CnStudyApp.app.dbManager;
        getView();
        setListViewData();
        adapter = new TextBookAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        finishButton.setOnClickListener(this);
    }

    public void setListViewData() {
        List<Textbook> textbooks = dbManager.getTextbooks();

        for (Textbook textbook : textbooks) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", textbook.getId());
            map.put("name", textbook.getName());
            list.add(map);
        }
    }

    public void getView() {
        listView = (ListView) findViewById(R.id.textbook_list);
        finishButton = (Button) findViewById(R.id.finish);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    }

    @Override
    public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
        case R.id.finish:

            intent = new Intent(this, CnStudyActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            break;

        default:
            break;
        }

    }
}
